//
//  ViewController.m
//  HTML
//
//  Created by Edison Cui on 16/1/6.
//  Copyright © 2016年 Gnet. All rights reserved.
//

//网页JS需要实现下面的方法
/*
 function connectWebViewJavascriptBridge(callback) {
 if (window.WebViewJavascriptBridge) {
 callback(WebViewJavascriptBridge)
 } else {
 document.addEventListener('WebViewJavascriptBridgeReady', function() {
 callback(WebViewJavascriptBridge)
 }, false)
 }
 }
 //登陆成功调用一下下面的代码来发消息
 connectWebViewJavascriptBridge(function(bridge) {
	var data = '消息'
	bridge.callHandler('标识符', data, function(response) { })
 }
 */

#import "LoginViewController.h"
#import "WebViewJavascriptBridge.h"//js与OC交互框架
#import "GnetQM_XMPPManager.h"

@interface LoginViewController () <UIWebViewDelegate>
@property (nonatomic, strong) UIWebView *webView;

@property (nonatomic, copy) NSString *baseURL;
@property (nonatomic, copy) NSString *logoImageURL;

@property (nonatomic, copy) NSString *loginURL;
@property (nonatomic, copy) NSString *registerURL;
@property (nonatomic, copy) NSString *forgetPasswordURL;

@property (nonatomic, strong) NSDictionary *userInfoDict;
@property (nonatomic, strong) WebViewJavascriptBridge *bridge;
@end

@implementation LoginViewController

- (void)viewDidLoad {
	[super viewDidLoad];

	if (_bridge) return;
	
	UIWebView* webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
	webView.backgroundColor = [UIColor whiteColor];
	self.webView = webView;
	[self.view addSubview:webView];
	NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:self.loginURL] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:5];
	_bridge = [WebViewJavascriptBridge bridgeForWebView:webView webViewDelegate:self handler:nil];
	[_bridge registerHandler:@"userLoginInfo" handler:^(id data, WVJBResponseCallback responseCallback) {
		NSLog(@"登陆页面有返回值");
		[self JSSerialize:data];
	}];
	[_bridge registerHandler:@"userRegisterInfo" handler:^(id data, WVJBResponseCallback responseCallback) {
		NSLog(@"注册页面有返回值");
		//点击注册按钮也处理返回的数据
		[self JSSerialize:data];
	}];

	[_webView loadRequest:request];
}

-(void) JSSerialize:(id)data {
	//json字符串返回的data是NSString类型的
	id dict = [NSJSONSerialization JSONObjectWithData:[data dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableLeaves error:nil];

	NSLog(@"%@",data);//打印数据
	if ([dict isKindOfClass:[NSDictionary class]]) {
		self.userInfoDict = dict;
		[[NSUserDefaults standardUserDefaults]setObject:dict forKey:DEFAULT_USER_INFO];
		NSString *userHeadImageURL = [dict objectForKey:@"USERHEAD_URL"];
		NSString *userID = [dict objectForKey:@"USER_ID"];
		//设置账户名 密码 注册 (注意：***注册的openfire的密码就是123456***)
		[[NSUserDefaults standardUserDefaults]setObject:userID forKey:kXMPPmyJID ];
		[[NSUserDefaults standardUserDefaults] setObject:@"123456" forKey:kXMPPmyPassword];
		//注册单点登录
		[[GnetQM_XMPPManager sharedInstance]registerMothod:^(BOOL ret) { }];
		NSLog(@"数据解析成功");
		[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:userHeadImageURL]]];
	} else {
		NSLog(@"数据解析失败");
	}
}

#pragma mark- UIWebViewDelegate
//判断跳转链接,重定向
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
	
	if ([[request URL] isEqual:[NSURL URLWithString:[NSString stringWithFormat:@"%@/Gnet_Ibd_Iom/page/fow/register",self.baseURL]]]) {
		NSURLRequest *registerRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:self.registerURL]];
		[self.webView loadRequest:registerRequest];
	} else if ([[request URL] isEqual:[NSURL URLWithString:[NSString stringWithFormat:@"%@/Gnet_Ibd_Iom/page/fow/login",self.baseURL]]]) {
		NSURLRequest *registerRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:self.loginURL]];
		[self.webView loadRequest:registerRequest];
	} else if ([[request URL] isEqual:[NSURL URLWithString:[NSString stringWithFormat:@"%@/Gnet_Ibd_Iom/page/fow/updatePassword",self.baseURL]]]) {
		NSURLRequest *registerRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:self.forgetPasswordURL]];
		[self.webView loadRequest:registerRequest];
	}
	return YES;
}

-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
	NSLog(@"==登=陆=页=面=加=载=失=败==\n======%@======",error);
}



#pragma mark- URL(自定义跳转的链接)
-(NSString *)baseURL {
	if (nil == _baseURL) _baseURL = Define_HTML_Login_HTTP;
	return _baseURL;
}
-(NSString *)logoImageURL {
	if(nil == _logoImageURL) _logoImageURL = Define_HTML_LOGO_IMAGE_URL;
	return _logoImageURL;
}

//登陆页面的URL
-(NSString *)loginURL {
	if (nil == _loginURL) {
		NSString *URL = @"Gnet_Ibd_Iom/page/fow/login";
		NSString *typeURL = @"ismobilelogin=0";//有代表手机端登陆,登陆成功发送JS,没有或者不为0则是普通登陆,会跳转页面
		_loginURL = [NSString stringWithFormat:@"%@/%@?logo=%@&%@", self.baseURL, URL, self.logoImageURL, typeURL];
	}
	return _loginURL;
}
//注册跳转到指定URL
-(NSString *)registerURL {
	if (nil == _registerURL) {
		NSString *URL = @"Gnet_Ibd_Iom/page/fow/register";
		NSString *cloudID = Define_HTML_CLOUD_ID;
		NSString *typeURL = @"isdefault=0";//0设置成默认私有云
		_registerURL = [NSString stringWithFormat:@"%@/%@?logo=%@&%@&%@", self.baseURL, URL, self.logoImageURL, cloudID, typeURL];
	}
	return _registerURL;
}
//忘记密码跳转到指定URL
-(NSString *)forgetPasswordURL {
	if (nil == _forgetPasswordURL) {
		NSString *URL = @"Gnet_Ibd_Iom/page/fow/updatePassword";
		_forgetPasswordURL = [NSString stringWithFormat:@"%@/%@?logo=%@", self.baseURL, URL, self.logoImageURL];
	}
	return _forgetPasswordURL;
}

@end
